const WXAPI = require('../../Wxapi/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: false,
    page: 1,
    limit: 20,

    append: false,
    is_over: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this,
        aid = options.aid,
        uid = options.uid,
        page = that.data.page,
        limit = that.data.limit;
    // 获取投票记录
    that.getVoteActionList(aid, uid, page, limit)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this,
        limit = that.data.limit,
        page = that.data.page*1 + 1,
        aid = wx.getStorageSync("activeInfo").id,
        uid = wx.getStorageSync("uid");

    that.getVoteActionList(aid, uid, page, limit)
  },

  getVoteActionList: function(aid, uid, page, limit){
    let that = this,
        append = that.data.append,
        list = that.data.list; 
    wx.showLoading({
      title: '拼命加载中...'
    })    
    WXAPI.getVoteActionList({
      aid: aid,
      uid: uid,
      page: page,
      limit: limit
    }).then(function (res) {
      if(append === false){
        if (res.code === 200) {
          that.setData({
            list: res.list,
            append: true
          })
        }
      } else {
        if(res.code === 200){
          for (let i in res.list) {
            list.push(res.list[i])
          }
          that.setData({
            list: list,
            is_over: false,
            page: page
          })
        } else {
          if(list.length > 0){
            that.setData({
              is_over: true
            })
          }
        }

      }
    }).finally(function(){
      wx.hideLoading()
    })
  }

})